package com.yu.rabbitmq.seven;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.yu.rabbitmq.utils.RabbitMqUnits;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

/**
 * 死信队列生产者
 */
public class DieMessageQueueProducer {

    private static final String NORMAL_EXCHANGE = "normal_exchange";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqUnits.getChannel();
        channel.exchangeDeclare(NORMAL_EXCHANGE, BuiltinExchangeType.DIRECT);
        //设置过期时间TTL = Time To Live 生存时间   时间单位是ms，毫秒
        AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder().expiration("10000").build();
        //将消息发给消费者，如果十秒后没人接收，就会到死信队列里面去
        for (int  i= 0; i <10 ;i ++) {
            channel.basicPublish(NORMAL_EXCHANGE,"zhangsan",properties,("info"+i).getBytes(StandardCharsets.UTF_8));
            System.out.println("生产者发送消息:info" + i);
        }

    }
}
